package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundCodec;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.system.Security;
	
	import org.grill_fish.fla.basics.*;
	import org.grill_fish.fla.basics.MyPlayer;
	import org.grill_fish.fla.events.*;
	import org.grill_fish.fla.utils.*;
	
	public class MusicPlayer extends Sprite
	{
		private var mp:MyPlayer = new MyPlayer();
		private var pos:int = 0;
		public function MusicPlayer()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init():void{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			flash.system.Security.allowDomain("*");
//                        flash.system.checkPolicyFile = true;
			this.addCallback('play', do_play);
			this.addCallback('pause', do_pause);
			this.addCallback('resume', do_resume);
			this.addCallback('set_src', do_setsrc);
                        this.addCallback('mute_volumn', do_mute);
                        this.addCallback('set_volumn', do_volumn);
                        mp.addEventListener(GF_VOLUMN_MUTEEVENT.eventname, mute);
			ExternalInterface.call("debug", "stand by");
		}

                private function mute(e:GF_VOLUMN_MUTEEVENT):void{
                    ExternalInterface.call('set_volumn', e.eventdata.gfvalue);
                }

                private function do_volumn(v:Number):void{
                    mp.changeVolumn(v);
                }
		
		private function do_setsrc(e:String):void{
			mp.setFileName(e);
		}
		
		private function do_pause():void{
			pos = mp.pause();
		}
		
		private function do_play():void{
			// pos = mp.pause();
			mp.play(0);
		}

                private function do_mute(a:Boolean = true):void{
                    mp.muteVolumn(a);
                }
		
		private function do_resume():void{
			mp.play(pos);
		}
		
		private function addCallback(functionName:String, closure:Function): void {
			if (ExternalInterface.available){
				ExternalInterface.addCallback(functionName,	closure);
			}
		}
	}
}
